package com.my.chapter13;

import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class practice16 {
    public static void main(String[] args) {
        if(args.length < 2) {
            System.out.println("Usage: java E16_JGrep3 file regex");
            System.exit(0);
        }
        File file = new File(args[0]);
        File[] files = null;
        if(file.isDirectory()) files = file.listFiles();
        else files = new File[]{new File(args[0])};
        Pattern p = Pattern.compile(args[1]);
        Matcher m = p.matcher("");
        for(File f : files) {
            System.out.println("-- File:" + f.getName() + "--");
            // Iterate through the lines of the input file:
            int index = 0;
            // new TextFile(f.getAbsolutePath())
            String[] lines = new String[10];
            for(String line : lines) {
                m.reset(line);
                while(m.find())
                    System.out.println(index++ + ": " +
                            m.group() + ": " + m.start());
            }
        }
    }
}

